using System;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;

namespace Framework.Portal.Layouts.Admin.Components.Database
{
    public partial class ViewsProperty : BaseControl
    {
        //_View objView = new _View();
        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AdminPage.CurrentCommand.CommandID)) || !CommandController.IsAvailableForUser(AdminPage.CurrentCommand.CommandID, CurrentUser.UserID, Convert.ToInt32(cfg.Portal)))
            //    Response.Redirect(cfg.ADMIN_CMD, true);
            //if (!IsPostBack)
            //{
            //    dropViews.DataSource = objView.GetViews();
            //    dropViews.DataTextField = "completename";
            //    dropViews.DataValueField = "id";
            //    dropViews.DataBind();

            //    ListItem myItem = dropViews.Items.FindByValue(Request.QueryString["id"].ToString());
            //    if (myItem != null)
            //        myItem.Selected = true;

            //    objView.ID = Convert.ToInt64(dropViews.SelectedValue);
            //    objView.LoadProperties();

            //    //load properties
            //    loadProperties(objView);

            //    lblCommandName.Text = MENUNAME;
            //}
        }

        protected void dropViews_SelectedIndexChanged(object sender, EventArgs e)
        {
            //objView.ID = Convert.ToInt64(dropViews.SelectedValue);
            //objView.LoadProperties();
            //loadProperties(objView);
        }

        //private void loadProperties(_View objView)
        //{
        //    loadColumns(objView);
        //    loadDefinition(objView);
        //    loadDepencecies(objView);

        //    //Navigation Urls
        //    lnkViewsList.NavigateUrl = MiscUtility.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.View));
        //    lnkViewDrop.NavigateUrl = MiscUtility.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.ViewProperty)) + "&action=drop&id=" + objView.ID;
        //}

        //private void loadColumns(_View myView)
        //{
        //    gridColumns.DataSource = myView.GetColumns();
        //    gridColumns.DataBind();
        //}

        //private void loadDefinition(_View myView)
        //{
        //    txtDefinition.Text = "";
        //    objView.SetDefinition();
        //    while (myView.Definition.Read())
        //    {
        //        txtDefinition.Text += myView.Definition.GetString(0).ToString();
        //    }
        //}

        //private void loadDepencecies(_View myView)
        //{
        //    //Clear treeview
        //    treeDependencies.FindNode("Ref/V").ChildNodes.Clear();
        //    treeDependencies.FindNode("Ref/P").ChildNodes.Clear();
        //    treeDependencies.FindNode("Refd/U").ChildNodes.Clear();
        //    treeDependencies.FindNode("Refd/V").ChildNodes.Clear();
        //    treeDependencies.FindNode("Refd/P").ChildNodes.Clear();
        //    //treeDependencies.FindNode("Refd/FN").ChildNodes.Clear();
        //    //******************************
        //    foreach (DataRow objRow in myView.GetReferences().Rows)
        //    {
        //        TreeNode myNode = new TreeNode();
        //        switch (objRow["xtype"].ToString().Trim())
        //        {
        //            case "U":
        //                //table
        //                myNode.Text = objRow["completeName"].ToString();
        //                myNode.NavigateUrl = "~/modules/Databases/tables/table_properties.aspx?id=" + objRow["id"].ToString() + "&dbname=" + myView.Name;
        //                treeDependencies.FindNode(objRow["type"].ToString() + "/U").ChildNodes.Add(myNode);
        //                break;
        //            case "V":
        //                //view
        //                myNode.Text = objRow["completeName"].ToString();
        //                myNode.NavigateUrl = "view_properties.aspx?id=" + objRow["id"].ToString() + "&dbname=" + myView.Name;
        //                treeDependencies.FindNode(objRow["type"].ToString() + "/V").ChildNodes.Add(myNode);
        //                break;
        //            case "P":
        //                //procedure
        //                myNode.Text = objRow["completeName"].ToString();
        //                myNode.NavigateUrl = "~/modules/Databases/storedprocedure/proc_properties.aspx?id=" + objRow["id"].ToString() + "&dbname=" + myView.Name;
        //                treeDependencies.FindNode(objRow["type"].ToString() + "/P").ChildNodes.Add(myNode);
        //                break;
        //            case "FN":
        //                //scalar function
        //                myNode.Text = objRow["name"].ToString();
        //                myNode.NavigateUrl = "~/modules/Databases/functions/func_properties.aspx?id=" + objRow["id"].ToString() + "&dbname=" + myView.Name;
        //                treeDependencies.FindNode(objRow["type"].ToString() + "/FN").ChildNodes.Add(myNode);
        //                break;

        //        }
        //    }
        //}

        protected void gridColumns_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //DataRowView rowView = (DataRowView)e.Row.DataItem;
            //if (rowView != null)
            //{
            //    Label myLabel = (Label)e.Row.FindControl("lblNull");

            //    switch (rowView["isnullable"].ToString())
            //    {
            //        case "1":
            //            myLabel.Text = "True";
            //            break;
            //        case "0":
            //            myLabel.Text = "False";
            //            break;
            //    }

            //}
        }

        protected void btnUpdateDef_Click(object sender, EventArgs e)
        {
            //objView.ID = Convert.ToInt64(dropViews.SelectedValue);
            //objView.LoadProperties();
            //try
            //{
            //    //if backup needed create a copy
            //    if (checkBackup.Checked)
            //    {
            //        //rename current view
            //        objView.Rename();
            //    }
            //    else
            //    {
            //        //drop view
            //        objView.Drop();
            //    }
            //    //execute definition
            //    objView.ExecuteQuery(txtDefinition.Text);
            //}
            //catch (Exception ex)
            //{
            //    lblError.Visible = true;
            //    lblError.Text = "<p>" + ex.Message + "</p>";
            //}
        }
    }
}